import {getMultiData,getGoosData} from '../../service/home.js'
 

Page({
  data: {
    banners:[],
    recommends:[],
    item:['流行','新款','精选'],
    goos:{
      'pop':{page:0,list:[]},
      'new':{page:0,list:[]},
      'sell':{page:0,list:[]}
    },
    currentType:'pop',
    scrollTop:0
  },

  onLoad: function (options) {
    this._getMultiData()
    this._getGoosData('pop')
    this._getGoosData('new')
    this._getGoosData('sell')
  },
  //网路请求
  _getMultiData(){
    getMultiData().then(res => {
      //取出数据
      const banners = res.data.data.banner.list.map(item => {
        return item.image
      }) 
      //保存数据
      this.setData({
        banners,
        recommends:res.data.data.recommend.list
      })
    })
  },
  _getGoosData(type){
    //获取当前页面
    const page = this.data.goos[type].page+1
    getGoosData(type,page).then(res=>{
      //取出数据
      const list = res.data.data.list
      //保存数据
      const oldlist =  this.data.goos[type].list
      //添加数据
      oldlist.push(...list)
      //获得当前的数据类型
      const typeKey = `goos.${type}.list`
      //当前对的页码
      const pageKey = `goos.${type}.page`
      //改变数据
      this.setData({
        [typeKey]:oldlist,
        [pageKey]:page
      })
      
    })
  },


  itemcoick(e){
    const index = e.detail.index
    switch(index){
      case 0 :
        this.setData({
          currentType:'pop'
        });
        break;
      case 1 :
        this.setData({
          currentType:'new'
        });
        break;
      case 2 :
        this.setData({
          currentType:'sell'
        });
        break;
    }
  },
  //页面滚动底部
  onReachBottom() {
    this._getGoosData(this.data.currentType)
  },
  onPageScroll(e){
    const scrollTop = e.scrollTop
    this.setData({
      scrollTop,
    })
  }
 
})